//
//  WJAppEnvironment.swift
//  NyamanDana
//
//  Created by 李永彬 on 2025/10/24.
//

import Foundation

import Foundation

// MARK: - 应用环境管理
final class WJAppEnvironment {
    
    // MARK: - 单例
    static let shared = WJAppEnvironment()
    private init() {}
    
    // MARK: - 公共属性
    var currentCountry: WJAppCountry {
        guard let countryCode = NDUserDefaults.shared.retrieveText(for: NDRegionCodeKey) else {
            return .default
        }
        return WJAppCountry(rawValue: countryCode) ?? .default
    }
    
    var countryNumberCode: String {
        return currentCountry.numberCode
    }
    
    func isValidCountryCode(_ code: String) -> Bool {
        return WJAppCountry(rawValue: code) != nil
    }
    
    var languageCode: String {
        return currentCountry.languageCode
    }
}

// MARK: - 国家枚举
enum WJAppCountry: String, CaseIterable {
    case indonesia = "931"
    case india = "379"
    
    // 默认国家
    static var `default`: WJAppCountry {
        return .india
    }
    
    // MARK: - 基本信息
    var numberCode: String {
        switch self {
        case .indonesia: return "62"
        case .india: return "91"
        }
    }

    // MARK: - 初始化
    init?(rawValue: String) {
        switch rawValue {
        case "931":
            self = .indonesia
        case "379":
            self = .india
        default:
            return nil
        }
    }
    
    var languageCode: String {
        switch self {
        case .indonesia:
            return "id"
        case .india:
            return "en"
        }
    }
}

// MARK: - 扩展
extension WJAppCountry: Equatable {
    static func == (lhs: WJAppCountry, rhs: WJAppCountry) -> Bool {
        return lhs.rawValue == rhs.rawValue
    }
}
